package app.joerhfam;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectHandler;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class ChessAppActivity extends Activity {
	
	private static final int MENU_UPDATE = Menu.FIRST;
	private static final int MENU_PREFERENCES = Menu.FIRST + 1;
	
	
	private static final int SHOW_PREFERENCES = 1;
	
	
	private	String username = "";
	private String password = "";
	private TextView tv;
	

	/** Called when the activity is first created. */
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		tv = (TextView)findViewById(R.id.text_view);
		//postData();
		tv.setText("Something");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_UPDATE, Menu.NONE, R.string.menu_update);
		menu.add(0, MENU_PREFERENCES, Menu.NONE, R.string.menu_preferences);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		
		switch(item.getItemId()){
		case MENU_UPDATE:
			updateChessInfo();
			return true;
		case MENU_PREFERENCES:
			Intent intent = new Intent(this, Preferences.class);
			startActivityForResult(intent, SHOW_PREFERENCES);
			return true;
			
		default:
			return false;
		}
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode == SHOW_PREFERENCES){
			//if(resultCode == Activity.RESULT_OK){
				updatePreferences();
			//}
		}
	}
	
	private void updatePreferences() {
		Context context = getApplicationContext();
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		username = prefs.getString(Preferences.PREF_LOGINNAME, "");
		password = prefs.getString(Preferences.PREF_PASSWORD, "");
		//TODO REMOVE NEXT STATEMENT
		tv.setText("Username: " + username +"\nPassword: " + password);
	}

	private void updateChessInfo() {
		//TODO REMOVE NEXT STATEMENT
		tv.setText("HOR!!");
	}

	
	
	
	private void postData() {

		DefaultHttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter("http.protocol.content-charset",
				"iso-8859-1");
		httpclient.setRedirectHandler(new DefaultRedirectHandler() {
			@Override
			public boolean isRedirectRequested(HttpResponse response,
					HttpContext context) {
				boolean isRedirect = super.isRedirectRequested(response,
						context);
				if (!isRedirect) {
					int responseCode = response.getStatusLine().getStatusCode();
					if (responseCode == 301 || responseCode == 302) {
						return true;
					}
				}
				return isRedirect;
			}
		});

		List<NameValuePair> params = new ArrayList<NameValuePair>(2);
		params.add(new BasicNameValuePair("username", username));
		params.add(new BasicNameValuePair("password", password));
		params.add(new BasicNameValuePair("c_", "CHS_MyPageController"));
		params.add(new BasicNameValuePair("m_", "submitMyPageForm"));
		params.add(new BasicNameValuePair("cid", "403"));
		params.add(new BasicNameValuePair("submitLogin", "Logg inn"));

		HttpPost request = new HttpPost("http://www.chess.no/");
		try {
			request.setEntity(new UrlEncodedFormEntity(params));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		HttpResponse response;
		try {
			response = httpclient.execute(request);
			HttpEntity content = response.getEntity();

			InputStream in = content.getContent();
			
			if(in != null){
				in.close();
			}
		
		
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



	}
}